package java04;
//二分法查找
public class ArrayTest05 {
    public static void main(String[] args){
        int[] arr = {2,5,8,22,44,56,89};
        int index = binarySearch2(arr,100);
        System.out.println(index == -1 ? "该元素不存在！" : "该元素下标" + index);
    }
    public static int binarySearch2(int[] arr, int dest) {
        int begin = 0;//开始下标
        int end = arr.length - 1;//结束下标
        while(begin <= end){
            int mid = (begin + end) / 2;//中间元素下标
            if(arr[mid] == dest){
                return mid;
            }else if(arr[mid] < dest){
                begin = mid + 1;
            }else{
                end = mid - 1;
            }
        }
        return -1;
    }
}
